package com.avcdata.national.fragment;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avcdata.aoscustommade.R;
import com.avcdata.aoscustommade.activity.Login_Activity;
import com.avcdata.aoscustommade.adapter.MyWheelAdapter;
import com.avcdata.aoscustommade.utils.AvcConstants;
import com.avcdata.aoscustommade.utils.SharedPreferencesUtils;
import com.avcdata.national.activity.ScaleBasisRatioActivity;
import com.avcdata.national.adapter.PriceAdapter;
import com.avcdata.national.module.StructureInfo;
import com.avcdata.national.network.BaseLog;
import com.avcdata.national.network.JsonRunnable;
import com.avcdata.national.network.ThreadPoolUtils;
import com.avcdata.national.utils.DrawHorizontalListView;
import com.avcdata.national.utils.MyBaseFragment;
import com.avcdata.national.utils.MyGrideView;
import com.google.gson.Gson;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.wx.wheelview.widget.WheelView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.avcdata.aoscustommade.R.id.dialog_cancel;
import static com.avcdata.aoscustommade.utils.AvcConstants.URL_structure;

/**
 * Created by Lucasey on 2017/8/22.
 */

public class StructureFragment extends MyBaseFragment {
    @ViewInject(R.id.sale_type)
    private LinearLayout sale_type;
    @ViewInject(R.id.sale_text)
    private TextView sale_text;
    @ViewInject(R.id.line_type)
    private LinearLayout line_type;
    @ViewInject(R.id.line_text)
    private TextView line_text;
    @ViewInject(R.id.week_type)
    private LinearLayout week_type;
    @ViewInject(R.id.week_text)
    private TextView week_text;
    @ViewInject(R.id.proportion_type)
    private LinearLayout proportion_type;
    @ViewInject(R.id.proportion_text)
    private TextView proportion_text;

    @ViewInject(R.id.price_tips)
    private LinearLayout price_tips;
    @ViewInject(R.id.product_tips)
    private LinearLayout product_tips;

    @ViewInject(R.id.product_lv)
    private MyGrideView product_lv;
    @ViewInject(R.id.price_lv)
    private MyGrideView price_lv;


    private PriceAdapter topAdapter;
    private PriceAdapter bottomAdapter;

    private LinearLayout loading;

    private String measureString = "online";
    private String weekString = "current";
    private String saleString = "amount";
    private String propString = "BasisRatio";
    private String measureStringTemp = "";
    private String weekStringTemp = "";
    private String saleStringTemp = "";
    private String propStringTemp = "";

    private Dialog dialog;
    private ArrayList<String> dataMeasure;
    private ArrayList<String> weeks;
    private ArrayList<String> sales;
    private ArrayList<String> props;

    private ScaleBasisRatioActivity scaleBasisRatioActivity;

    private List<String> userbrands = new ArrayList<>();

    @Override
    protected View onLayoutView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_national_structure, container, false);
    }

    @Override
    protected void initView(View container, Bundle savedInstanceState) {
        ViewUtils.inject(this, container);
        week_type.setOnClickListener(this);
        line_type.setOnClickListener(this);
        sale_type.setOnClickListener(this);
        proportion_type.setOnClickListener(this);
        product_lv.setSelector(new ColorDrawable(Color.TRANSPARENT));
        price_lv.setSelector(new ColorDrawable(Color.TRANSPARENT));
        scaleBasisRatioActivity = (ScaleBasisRatioActivity) getActivity();
        loading = scaleBasisRatioActivity.getLoading();
        userbrands = new ArrayList<>(new SharedPreferencesUtils(scaleBasisRatioActivity).getCompetingBrands(scaleBasisRatioActivity.getCategory()));
        for (String str : userbrands) {
            BaseLog.e(scaleBasisRatioActivity.getCategory() + "：", "<===>" + str);
        }
        initOnCreate();
    }


    private void initOnCreate() {
        loading.setVisibility(View.GONE);
        dataMeasure = new ArrayList<>();
        dataMeasure.add("线上");
        dataMeasure.add("线下");
        props = new ArrayList<>();
        props.add("同比");
        props.add("环比");
        weeks = new ArrayList<>();
        weeks.add("当前周");
        weeks.add("近四周");
        weeks.add("近八周");
        weeks.add("本年累计");
        sales = new ArrayList<>();
        sales.add("销量");
        sales.add("销额");
        //设置线上线下方式
        if (new SharedPreferencesUtils(getActivity()).getLineType() == 1) {
            line_text.setText("线上");
            measureString = "online";
        } else {
            line_text.setText("线下");
            measureString = "offline";
        }
        //设置同比环比
        if (new SharedPreferencesUtils(getActivity()).getRatioType() == 1) {
            proportion_text.setText("同比");
            propString = "BasisRatio";
        } else {
            proportion_text.setText("环比");
            propString = "LinkRelativeRatio";
        }
        //设置周度
        switch (new SharedPreferencesUtils(getActivity()).getWeekType()) {
            case "近四周":
                week_text.setText("近四周");
                weekString = "fourWeek";
                break;
            case "近八周":
                week_text.setText("近八周");
                weekString = "eightWeek";
                break;
            case "本年累计":
                week_text.setText("本年累计");
                weekString = "year";
                break;
            case "当前周":
                week_text.setText("当前周");
                weekString = "current";
                break;
            default:
                week_text.setText("当前周");
                weekString = "current";
                break;
        }
        //设置销量销额方式
        if (new SharedPreferencesUtils(getActivity()).getSaleType() == 1) {
            sale_text.setText("销量");
            saleString = "volume";
        } else {
            sale_text.setText("销额");
            saleString = "amount";
        }

        getBrandScaleBasisRatio();
    }


    @Override
    protected void onclickEvent(View view) {
        switch (view.getId()) {
            case R.id.proportion_type:
                showDialogSelect(4);
                if (dialog != null && !dialog.isShowing()) {
                    dialog.show();
                }
                break;
            case R.id.sale_type:
                showDialogSelect(1);
                if (dialog != null && !dialog.isShowing()) {
                    dialog.show();
                }
                break;
            case R.id.line_type:
                showDialogSelect(3);
                if (dialog != null && !dialog.isShowing()) {
                    dialog.show();
                }
                break;
            case R.id.week_type:
                showDialogSelect(2);
                if (dialog != null && !dialog.isShowing()) {
                    dialog.show();
                }
                break;
            default:
                break;
        }
    }

    /***
     * 获取选择的索引
     * @param dataTemp
     * @param stringTemp
     * @return
     */
    private int getSelectPosition(ArrayList<String> dataTemp, String stringTemp) {
        int temp = 0;
        for (int i = 0; i < dataTemp.size(); i++) {
            if (dataTemp.get(i).equals(stringTemp)) {
                temp = i;
            }
        }
        return temp;
    }

    /***
     *单选dialog
     * @param dialogNum 3-线上线下/2-最新周···
     */
    private void showDialogSelect(final int dialogNum) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_single, null);
        dialog = new Dialog(getActivity(), R.style.DialogBlack);
        Window window = dialog.getWindow();
        window.setContentView(view);
        window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        window.setGravity(Gravity.CENTER);

        final TextView dialog_title1 = (TextView) view.findViewById(R.id.dialog_title1);
        TextView dialog_title2 = (TextView) view.findViewById(R.id.dialog_title2);
        final WheelView wheelview_single1 = (WheelView) view.findViewById(R.id.wheelview_single1);
        final WheelView wheelview_single2 = (WheelView) view.findViewById(R.id.wheelview_single2);
        dialog_title2.setVisibility(View.GONE);
        wheelview_single2.setVisibility(View.GONE);
        wheelview_single1.setSkin(WheelView.Skin.None);
        WheelView.WheelViewStyle style = new WheelView.WheelViewStyle();
        style.selectedTextColor = Color.RED;
        style.selectedTextSize = 20;
        style.textColor = Color.BLACK;
        wheelview_single1.setStyle(style);
        wheelview_single1.setLoop(false);
        wheelview_single1.setWheelClickable(false);
        switch (dialogNum) {
            case 1:
                dialog_title1.setText("选择销量销额");
                wheelview_single1.setWheelData(sales);
                wheelview_single1.setSelection(getSelectPosition(sales, saleStringTemp));
                wheelview_single1.setWheelSize(3);
                wheelview_single1.setWheelAdapter(new MyWheelAdapter(getActivity()));
                break;
            case 2:
                dialog_title1.setText("选择周度时长");
                wheelview_single1.setWheelData(weeks);
                wheelview_single1.setSelection(getSelectPosition(weeks, weekStringTemp));
                wheelview_single1.setWheelSize(3);
                wheelview_single1.setWheelAdapter(new MyWheelAdapter(getActivity()));
                break;
            case 3:
                dialog_title1.setText("选择销售状况");
                wheelview_single1.setWheelData(dataMeasure);
                wheelview_single1.setSelection(getSelectPosition(dataMeasure, measureStringTemp));
                wheelview_single1.setWheelSize(3);
                wheelview_single1.setWheelAdapter(new MyWheelAdapter(getActivity()));
                break;
            case 4:
                dialog_title1.setText("选择同比环比");
                wheelview_single1.setWheelData(props);
                wheelview_single1.setSelection(getSelectPosition(props, propStringTemp));
                wheelview_single1.setWheelSize(3);
                wheelview_single1.setWheelAdapter(new MyWheelAdapter(getActivity()));
                break;
            default:
                break;
        }

        view.findViewById(dialog_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });

        view.findViewById(R.id.dialog_ensure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (dialogNum) {
                    case 1:
                        saleStringTemp = sales.get(wheelview_single1.getCurrentPosition());
                        sale_text.setText(saleStringTemp);
                        if (sales.get(wheelview_single1.getCurrentPosition()).equals("销量")) {
                            saleString = "volume";
                            sale_text.setText("销量");
                            new SharedPreferencesUtils(getActivity()).setSaleType(1);
                        } else {
                            saleString = "amount";
                            sale_text.setText("销额");
                            new SharedPreferencesUtils(getActivity()).setSaleType(2);
                        }
                        break;
                    case 2:
                        weekStringTemp = weeks.get(wheelview_single1.getCurrentPosition());
                        week_text.setText(weekString);
                        switch (weekStringTemp) {
                            case "近四周":
                                week_text.setText("近四周");
                                weekString = "fourWeek";
                                new SharedPreferencesUtils(getActivity()).setWeekType("近四周");
                                break;
                            case "近八周":
                                week_text.setText("近八周");
                                weekString = "eightWeek";
                                new SharedPreferencesUtils(getActivity()).setWeekType("近八周");
                                break;
                            case "本年累计":
                                week_text.setText("本年累计");
                                weekString = "year";
                                new SharedPreferencesUtils(getActivity()).setWeekType("本年累计");
                                break;
                            case "当前周":
                                week_text.setText("当前周");
                                weekString = "current";
                                new SharedPreferencesUtils(getActivity()).setWeekType("当前周");
                                break;
                            default:
                                week_text.setText("当前周");
                                weekString = "current";
                                new SharedPreferencesUtils(getActivity()).setWeekType("当前周");
                                break;
                        }
                        break;
                    case 3:
                        measureStringTemp = dataMeasure.get(wheelview_single1.getCurrentPosition());
                        line_text.setText(measureString);
                        if (dataMeasure.get(wheelview_single1.getCurrentPosition()).equals("线上")) {
                            measureString = "online";
                            line_text.setText("线上");
                            new SharedPreferencesUtils(getActivity()).setLineType(1);
                        } else {
                            measureString = "offline";
                            line_text.setText("线下");
                            new SharedPreferencesUtils(getActivity()).setLineType(2);
                        }
                        break;
                    case 4:
                        propStringTemp = props.get(wheelview_single1.getCurrentPosition());
                        proportion_text.setText(propString);
                        if (props.get(wheelview_single1.getCurrentPosition()).equals("同比")) {
                            propString = "BasisRatio";
                            proportion_text.setText("同比");
                            new SharedPreferencesUtils(getActivity()).setRatioType(1);
                        } else {
                            propString = "LinkRelativeRatio";
                            proportion_text.setText("环比");
                            new SharedPreferencesUtils(getActivity()).setRatioType(2);
                        }
                        break;
                }
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
                getBrandScaleBasisRatio();
            }
        });
    }

    /**
     * 获取上半部分规模同比
     * api/scale/brandScaleBasisRatio
     */
    private void getBrandScaleBasisRatio() {
        loading.setVisibility(View.VISIBLE);
        HashMap<String, Object> map = new HashMap<>();
        map.put("salesType", saleString);
        map.put("linesType", measureString);
        map.put("timeType", weekString);
        StringBuffer sb = new StringBuffer();
        for (String str : userbrands) {
            sb.append(str + ",");
        }
        sb.deleteCharAt(sb.length() - 1);
        map.put("competingBrands", sb.toString());
        map.put("category", scaleBasisRatioActivity.getCategory());
        map.put("brand", "A.O.史密斯");
        map.put("lastWeek", new SharedPreferencesUtils(getActivity()).getNewWeek());
        ThreadPoolUtils.execute(new JsonRunnable(getActivity(), "HTTPGET", AvcConstants.URLHeaderBase + URL_structure, handler, map, 1));

    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case -1:
                    loading.setVisibility(View.GONE);
                    break;
                case 1:
                    loading.setVisibility(View.GONE);
                    StructureInfo si = new Gson().fromJson(msg.obj.toString(),StructureInfo.class);
                    setData(si);
                    if(weekString.equals("current")){
                        week_text.setText(new SharedPreferencesUtils(getActivity()).getNewWeek());
                    }
                    break;
                case -22:
                    loading.setVisibility(View.GONE);
                    Intent intent = new Intent ();
                    intent.setClass ( getActivity (), Login_Activity.class );
                    intent.setFlags ( Intent.FLAG_ACTIVITY_CLEAR_TASK );  //注意本行的FLAG设置
                    getActivity ().finish ();
                    startActivity ( intent );
                    break;
                default:
                    break;
            }
        }
    };

    /***
     * 设置网络数据到页面
     * @param si
     */
    private void setData(StructureInfo si){
        String[] productTypes = new String[si.getReturnValue().getProductTypes().size()];
        String[] priceSegments = new String[si.getReturnValue().getPriceSegments().size()];

        for(int i=0;i<si.getReturnValue().getProductTypes().size();i++){
            productTypes[i] = si.getReturnValue().getProductTypes().get(i).getProductType();
        }

        for(int i=0;i<si.getReturnValue().getPriceSegments().size();i++){
            priceSegments[i] = si.getReturnValue().getPriceSegments().get(i).getPriceSegment();
        }

        Map<String, Map<String, StructureInfo.ReturnValueBean.brandThenProductStructureBean>> mProductStructures = new HashMap<>();
        Map<String, Map<String, StructureInfo.ReturnValueBean.brandThenPriceSegmentBean>> mPriceSegment = new HashMap<>();

        mProductStructures = si.getReturnValue().getBrandThenProductStructure();
        mPriceSegment = si.getReturnValue().getBrandThenPriceSegment();

        Map<String,List<String>> map = new HashMap<>();
        List<String> allBrands = new ArrayList<>();
        allBrands.add("all");
        allBrands.add("A.O.史密斯");
        allBrands.addAll(userbrands);
        for(String str :allBrands){
            List<String> markets = new ArrayList<>();
            List<String> ratios = new ArrayList<>();
            List<String> prices = new ArrayList<>();
            List<String> pRatios = new ArrayList<>();
            //筛选产品结构
            for(int i=0;i<productTypes.length;i++) {
                String type = productTypes[i];
                if(i==0){
                    markets.add(str);
                    if(propString.equals("BasisRatio")){
                        ratios.add("同比");
                    }else{
                        ratios.add("环比");
                    }
                }
                if (null != mProductStructures.get(type)) {
                    if (null != mProductStructures.get(type).get(str)) {
                        if(propString.equals("BasisRatio")){
                            if(saleString.equals("volume")){
                                markets.add(mProductStructures.get(type).get(str).getVolumeStructureMarketShare()+"");
                                ratios.add(mProductStructures.get(type).get(str).getVolumeMarketShareBasisRatio()+"");
                            }else{
                                markets.add(mProductStructures.get(type).get(str).getAmountStructureMarketShare()+"");
                                ratios.add(mProductStructures.get(type).get(str).getAmountMarketShareBasisRatio()+"");
                            }
                        }else{
                            if(saleString.equals("volume")){
                                markets.add(mProductStructures.get(type).get(str).getVolumeStructureMarketShare()+"");
                                ratios.add(mProductStructures.get(type).get(str).getVolumeMarketShareLinkRelativeRatio()+"");
                            }else{
                                markets.add(mProductStructures.get(type).get(str).getAmountStructureMarketShare()+"");
                                ratios.add(mProductStructures.get(type).get(str).getAmountMarketShareLinkRelativeRatio()+"");
                            }
                        }
                    }else{
                        markets.add("null");
                        ratios.add("null");
                    }
                } else {
                    markets.add("null");
                    ratios.add("null");
                }
            }
            //筛选价格段结构
            for(int i=0;i<priceSegments.length;i++) {
                String type = priceSegments[i];
                if(i==0){
                    prices.add(str);
                    if(propString.equals("BasisRatio")){
                        pRatios.add("同比");
                    }else{
                        pRatios.add("环比");
                    }
                }
                if (null != mPriceSegment.get(type)) {
                    if (null != mPriceSegment.get(type).get(str)) {
                        if(propString.equals("BasisRatio")){
                            if(saleString.equals("volume")){
                                prices.add(mPriceSegment.get(type).get(str).getVolumeMarketShare()+"");
                                pRatios.add(mPriceSegment.get(type).get(str).getVolumeMarketShareBasisRatio()+"");
                            }else{
                                prices.add(mPriceSegment.get(type).get(str).getAmountMarketShare()+"");
                                pRatios.add(mPriceSegment.get(type).get(str).getAmountMarketShareBasisRatio()+"");
                            }
                        }else{
                            if(saleString.equals("volume")){
                                prices.add(mPriceSegment.get(type).get(str).getVolumeMarketShare()+"");
                                pRatios.add(mPriceSegment.get(type).get(str).getVolumeMarketShareLinkRelativeRatio()+"");
                            }else{
                                prices.add(mPriceSegment.get(type).get(str).getAmountMarketShare()+"");
                                pRatios.add(mPriceSegment.get(type).get(str).getAmountMarketShareLinkRelativeRatio()+"");
                            }
                        }
                    }else{
                        prices.add("null");
                        pRatios.add("null");
                    }
                } else {
                    prices.add("null");
                    pRatios.add("null");
                }
            }
            map.put(str+"_market",markets);
            map.put(str+"_ratios",ratios);
            map.put(str+"_prices",prices);
            map.put(str+"_pRatios",pRatios);
        }
        BaseLog.e("????",new Gson().toJson(map));

        //数据重组完毕，进行数据加载
        //获取表头
        for(int i=0;i<si.getReturnValue().getProductTypes().size();i++){
            productTypes[i] = si.getReturnValue().getProductTypes().get(i).getProductType();
        }
        for(int i=0;i<si.getReturnValue().getPriceSegments().size();i++){
            priceSegments[i] = si.getReturnValue().getPriceSegments().get(i).getPriceSegment();
        }
        final DrawHorizontalListView topTitle = new DrawHorizontalListView ( getActivity (), dip2px ( getActivity (), 1 ), productTypes, 1 ,false);
        LinearLayout.LayoutParams topTitle_params1 = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT );

        final DrawHorizontalListView bottomTitle = new DrawHorizontalListView ( getActivity (), dip2px ( getActivity (), 1 ), priceSegments, 1 ,false);
        LinearLayout.LayoutParams bottomTitle_params1 = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT );
        //加载前四列
//        final DrawHorizontalListView topColu1 = new DrawHorizontalListView ( getActivity (), dip2px ( getActivity (), 1 ), map.get("all_market").toArray(new String[map.get("all_market").size()]), 2 );
//        LinearLayout.LayoutParams topColu1_params1 = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT );
//        final DrawHorizontalListView topColu2 = new DrawHorizontalListView ( getActivity (), dip2px ( getActivity (), 1 ), map.get("all_ratios").toArray(new String[map.get("all_ratios").size()]), 2 );
//        LinearLayout.LayoutParams topColu2_params2 = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT );
        final DrawHorizontalListView topColu3 = new DrawHorizontalListView ( getActivity (), dip2px ( getActivity (), 1 ), map.get("A.O.史密斯_market").toArray(new String[map.get("A.O.史密斯_market").size()]), 2 ,true);
        LinearLayout.LayoutParams topColu3_params3 = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT );
        final DrawHorizontalListView topColu4 = new DrawHorizontalListView ( getActivity (), dip2px ( getActivity (), 1 ), map.get("A.O.史密斯_ratios").toArray(new String[map.get("A.O.史密斯_ratios").size()]), 2 ,false);
        LinearLayout.LayoutParams topColu4_params4 = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT );

//        final DrawHorizontalListView bottomColu1 = new DrawHorizontalListView ( getActivity (), dip2px ( getActivity (), 1 ), map.get("all_prices").toArray(new String[map.get("all_prices").size()]), 2 );
//        LinearLayout.LayoutParams bottomColu1_params1 = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT );
//        final DrawHorizontalListView bottomColu2 = new DrawHorizontalListView ( getActivity (), dip2px ( getActivity (), 1 ), map.get("all_pRatios").toArray(new String[map.get("all_pRatios").size()]), 2 );
//        LinearLayout.LayoutParams bottomColu2_params2 = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT );
        final DrawHorizontalListView bottomColu3 = new DrawHorizontalListView ( getActivity (), dip2px ( getActivity (), 1 ), map.get("A.O.史密斯_prices").toArray(new String[map.get("A.O.史密斯_prices").size()]), 2 ,true);
        LinearLayout.LayoutParams bottomColu3_params3 = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT );
        final DrawHorizontalListView bottomColu4 = new DrawHorizontalListView ( getActivity (), dip2px ( getActivity (), 1 ), map.get("A.O.史密斯_pRatios").toArray(new String[map.get("A.O.史密斯_pRatios").size()]), 2 ,false);
        LinearLayout.LayoutParams bottomColu4_params4 = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT );

        //通知view组件重绘
        topTitle.invalidate ();
        bottomTitle.invalidate ();
        product_tips.removeAllViews ();
        price_tips.removeAllViews ();
        product_tips.addView ( topTitle, topTitle_params1 );
//        product_tips.addView ( topColu1, topColu1_params1 );
//        product_tips.addView ( topColu2, topColu2_params2 );
        product_tips.addView ( topColu3, topColu3_params3 );
        product_tips.addView ( topColu4, topColu4_params4 );
        price_tips.addView ( bottomTitle, bottomTitle_params1 );
//        price_tips.addView ( bottomColu1, bottomColu1_params1 );
//        price_tips.addView ( bottomColu2, bottomColu2_params2 );
        price_tips.addView ( bottomColu3, bottomColu3_params3 );
        price_tips.addView ( bottomColu4, bottomColu4_params4 );
        allBrands.remove("A.O.史密斯");
        topAdapter = new PriceAdapter(getActivity(),allBrands,map,true);
        bottomAdapter = new PriceAdapter(getActivity(),allBrands,map,false);
        product_lv.setAdapter(topAdapter);
        price_lv.setAdapter(bottomAdapter);

        int length = allBrands.size ();
        DisplayMetrics dm = new DisplayMetrics ();
        getActivity ().getWindowManager ().getDefaultDisplay ().getMetrics ( dm );
        float density = dm.density;
        int gridviewWidth = (int) (136 * length * density);
        int itemWidth = (int) (134 * density);//根据屏幕密度和你的数据长度设置gridView的宽度，和每个item的宽度
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams ( gridviewWidth, LinearLayout.LayoutParams.FILL_PARENT );
        product_lv.setLayoutParams ( params ); //重点
        product_lv.setColumnWidth ( itemWidth ); //重点
        product_lv.setHorizontalSpacing ( 10 ); //间距
        product_lv.setStretchMode ( GridView.NO_STRETCH );
        product_lv.setNumColumns ( length ); //重点
        price_lv.setLayoutParams ( params ); //重点
        price_lv.setColumnWidth ( itemWidth ); //重点
        price_lv.setHorizontalSpacing ( 10 ); //间距
        price_lv.setStretchMode ( GridView.NO_STRETCH );
        price_lv.setNumColumns ( length ); //重点

    }


    /**
     * 根据手机分辨率从dp转到px
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources ().getDisplayMetrics ().density;
        return (int) (dpValue * scale + 0.5f);
    }


}